//
//  HDCFRiskTestCel.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/4.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFRiskTestCel.h"
#import "HomeBtn.h"

#define LabelWidth screenWidth - 75
#define ButtonLabelWidth screenWidth - 130

@interface HDCFRiskTestCel ()
@property (weak, nonatomic) IBOutlet UILabel *questionTab;
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@property (weak, nonatomic) IBOutlet HomeBtn *btnOne;
@property (weak, nonatomic) IBOutlet HomeBtn *btnTwo;
@property (weak, nonatomic) IBOutlet HomeBtn *btnThree;
@property (weak, nonatomic) IBOutlet HomeBtn *btnFour;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (nonatomic, strong) NSArray *btnArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnOneHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnTwoHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnThreeHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnFourHeight;


@end

@implementation HDCFRiskTestCel

- (void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    if (isSelect == NO) {
        for (int i = 0; i < self.btnArray.count; i++) {
            HomeBtn *btn = self.btnArray[i];
            [btn setImage:[UIImage imageNamed:@"button_pinggu_dis"] forState:UIControlStateNormal];
        }
    }
}

- (void)setSelectIndex:(NSString *)selectIndex{
    _selectIndex = selectIndex;
    if ([selectIndex isEqualToString:@"a"]) {
        [self setBtnWithIndex:0];
    }else if ([selectIndex isEqualToString:@"b"]){
        [self setBtnWithIndex:1];
    }else if ([selectIndex isEqualToString:@"c"]){
        [self setBtnWithIndex:2];
    }else if ([selectIndex isEqualToString:@"d"]){
        [self setBtnWithIndex:3];
    }else{
        [self setBtnW];
    }
}

- (void)setBtnW{
    for (int i = 0; i < self.btnArray.count; i++) {
        HomeBtn *btn = self.btnArray[i];
            [btn setImage:[UIImage imageNamed:@"button_pinggu_dis"] forState:UIControlStateNormal];
        }
}

- (void)setBtnWithIndex:(NSInteger)index{
    
    for (int i = 0; i < self.btnArray.count; i++) {
        HomeBtn *btn = self.btnArray[i];
        if (i == index) {
             [btn setImage:[UIImage imageNamed:@"button_pinggu_sel"] forState:UIControlStateNormal];
        }else{
             [btn setImage:[UIImage imageNamed:@"button_pinggu_dis"] forState:UIControlStateNormal];
        }
       
    }
   
}

- (void)setIndex:(NSInteger)index{
    _index = index;
    self.questionTab.text = [NSString stringWithFormat:@"%ld/12", index + 1];
}

- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    
    CGFloat normalHeight = 14.32;
    
    NSString *title = dict[@"title"];
    self.questionLabel.text = title;
    
    NSDictionary *optionDict = dict[@"option"];
    NSDictionary *optionA = optionDict[@"a"];
    
    CGFloat oneHeight = [self sizeWithText:optionA[@"content"] font:[UIFont systemFontOfSize:13] maxW:ButtonLabelWidth];
    self.btnOneHeight.constant = oneHeight / normalHeight * 20;
    [self.btnOne setTitle:optionA[@"content"] forState:UIControlStateNormal];
    
    NSDictionary *optionB = optionDict[@"b"];
    
    CGFloat twoHeight = [self sizeWithText:optionB[@"content"] font:[UIFont systemFontOfSize:13] maxW:ButtonLabelWidth];
    self.btnTwoHeight.constant = twoHeight / normalHeight * 20;
    [self.btnTwo setTitle:optionB[@"content"] forState:UIControlStateNormal];
    
    NSDictionary *optionC = optionDict[@"c"];
    
     CGFloat threeHeight = [self sizeWithText:optionC[@"content"] font:[UIFont systemFontOfSize:13] maxW:ButtonLabelWidth];
    self.btnThreeHeight.constant = threeHeight / normalHeight * 20;
    [self.btnThree setTitle:optionC[@"content"] forState:UIControlStateNormal];
    
    NSDictionary *optionD = optionDict[@"d"];
    
    CGFloat fourHeight = [self sizeWithText:optionD[@"content"] font:[UIFont systemFontOfSize:13] maxW:ButtonLabelWidth];
    self.btnFourHeight.constant = fourHeight / normalHeight * 20;
    [self.btnFour setTitle:optionD[@"content"] forState:UIControlStateNormal];
}

- (NSArray *)btnArray{
    if (!_btnArray) {
        self.btnArray = [NSArray arrayWithObjects:self.btnOne, self.btnTwo, self.btnThree, self.btnFour, nil];
    }
    return _btnArray;
}

+ (instancetype)HDCFRiskTestCel{
    return  [[NSBundle mainBundle] loadNibNamed:@"HDCFRiskTestCel" owner:nil options:nil].firstObject;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backView.layer.borderWidth = 1.0;
    self.backView.layer.cornerRadius = 2.0;
    self.backView.layer.borderColor = ZKcolor(230, 230, 230).CGColor;
    self.backView.clipsToBounds = YES;
    
    UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:self.questionTab.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(6, 6)];
    CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];
    maskLayer.frame=self.questionTab.bounds;
    maskLayer.path=maskPath.CGPath;
    self.questionTab.layer.mask=maskLayer;
    self.questionTab.clipsToBounds = YES;
    
    self.btnOne.titleLabel.lineBreakMode = 0;
    self.btnTwo.titleLabel.lineBreakMode = 0;
    self.btnThree.titleLabel.lineBreakMode = 0;
    self.btnFour.titleLabel.lineBreakMode = 0;
}

//计算高度
- (CGFloat)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.height;
    
}

- (IBAction)btnOneClick:(HomeBtn *)sender {
    [self setImgWithBtn:sender];
    [self setIndex:_index andOption:@"a"];
}
- (IBAction)btnTwoClick:(HomeBtn *)sender {
    [self setImgWithBtn:sender];
    [self setIndex:_index andOption:@"b"];
}
- (IBAction)btnThreeClick:(HomeBtn *)sender {
    [self setImgWithBtn:sender];
    [self setIndex:_index andOption:@"c"];
}
- (IBAction)btnFourClick:(HomeBtn *)sender {
    [self setImgWithBtn:sender];
    [self setIndex:_index andOption:@"d"];
}

- (void)setIndex:(NSInteger)index andOption:(NSString *)option{
    if ([self.delegate respondsToSelector:@selector(returnTithQuestionTab:WithIndex:)]) {
        [self.delegate returnTithQuestionTab:option WithIndex:index];
    }
}

- (void)setImgWithBtn:(HomeBtn *)sender{
    for (int i = 0; i < self.btnArray.count; i++) {
        HomeBtn *btn = self.btnArray[i];
        [btn setImage:[UIImage imageNamed:@"button_pinggu_dis"] forState:UIControlStateNormal];
    }
    [sender setImage:[UIImage imageNamed:@"button_pinggu_sel"] forState:UIControlStateNormal];
}
@end
